﻿using Kingdee.BOS;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace CB.KM.ZH.PayableClearInvoiceQty
{
    public class K3Helper
    {
        public static string url = CommonData.url;
        //public static string dbId = CommonData.dbId;
        public static string appID = CommonData.appID;
        public static string appSec = CommonData.appSec;
        public static string userName = CommonData.userName;
        public static string password = CommonData.password;

        public static int type = 2052;

        public static bool Login(Context ctx)
        {
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginByAppSecret.common.kdsvc");
                List<object> Parameters = new List<object>();
                Parameters.Add(ctx.DBId);//账套
                Parameters.Add(userName);//用户名
                Parameters.Add(appID);//AppID
                Parameters.Add(appSec);//AppSec
                Parameters.Add(2052);//2052代表中文
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//登录结果
                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                return obj["LoginResultType"].ToStringV1() == "1";
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool Login(Context ctx,ref string msg)
        {
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginByAppSecret.common.kdsvc");
                List<object> Parameters = new List<object>();
                Parameters.Add(ctx.DBId);//账套
                Parameters.Add(userName);//用户名
                Parameters.Add(appID);//AppID
                Parameters.Add(appSec);//AppSec
                Parameters.Add(2052);//2052代表中文
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//登录结果
                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = resultStr;
                return obj["LoginResultType"].ToStringV1() == "1";
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool Save(Context ctx, string formId, string content, ref string msg)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果

                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = obj["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(obj["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return Save(ctx, formId, content, ref msg);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool SaveG(Context ctx, string content, ref string msg)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
                List<object> Parameters = new List<object>();
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果

                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = obj["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(obj["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return SaveG(ctx,content, ref msg);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool SaveV1(Context ctx, string formId, string content, ref string msg, ref JObject returnData)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                returnData = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = returnData["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(returnData["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return SaveV1(ctx, formId, content, ref msg, ref returnData);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <param name="msg"></param>
        /// <param name="returnData"></param>
        /// <returns></returns>

        public static bool BatchSave(Context ctx, string formId, string content, ref string msg, ref JObject returnData)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                returnData = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = returnData["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(returnData["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return BatchSave(ctx, formId, content, ref msg, ref returnData);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 下推
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool Push(Context ctx, string formId, string content, ref string msg)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Push.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = obj["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(obj["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return Push(ctx, formId, content, ref msg);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 下推
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool PushV1(Context ctx, string formId, string content, ref string msg, ref JObject returnData)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Push.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                returnData = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = returnData["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(returnData["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return PushV1(ctx, formId, content, ref msg, ref returnData);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool Submit(Context ctx, string formId, string content, ref string msg)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = obj["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(obj["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return Submit(ctx, formId, content, ref msg);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool SubmitV1(Context ctx, string formId, string content, ref string msg, ref JObject returnData)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                returnData = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = returnData["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(returnData["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return SubmitV1(ctx, formId, content, ref msg, ref returnData);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool Check(Context ctx, string formId, string content, ref string msg)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = obj["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(obj["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return Check(ctx, formId, content, ref msg);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool CheckV1(Context ctx, string formId, string content, ref string msg, ref JObject returnData)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                returnData = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = returnData["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(returnData["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return CheckV1(ctx, formId, content, ref msg, ref returnData);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool Delete(Context ctx, string formId, string content, ref string msg)
        {
            bool isok = false;
            try
            {
                HttpClientV1 httpClient = new HttpClientV1();
                httpClient.Url = string.Concat(url, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc");
                List<object> Parameters = new List<object>();
                //业务对象Id 
                Parameters.Add(formId);
                //Json字串
                Parameters.Add(content);
                httpClient.Content = JsonConvert.SerializeObject(Parameters);
                string resultStr = httpClient.SysncRequest();//执行结果
                JObject obj = JsonConvert.DeserializeObject<JObject>(resultStr);
                msg = obj["Result"]["ResponseStatus"]["Errors"].ToString();
                isok = Convert.ToBoolean(obj["Result"]["ResponseStatus"]["IsSuccess"].ToString());
                if (msg.Contains("会话信息已丢失，请重新登录"))
                {
                    if (Login(ctx))
                        return Delete(ctx, formId, content, ref msg);
                    else
                        return false;
                }
                else
                {
                    return isok;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }
}
